package com.phone.sdktest.window;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public final class MiWindow implements IWindow {
    private static final String TAG = "MiWindow";

    private Toast toast;
    private boolean isShow = false;

    private Object mTN;
    private Method show;
    private Method hide;

    private static View mView;
    private static Context mContext;

    private MiWindow() {
    }


    public static final MiWindow getInstance(Context context, View view) {
        mContext = context;
        mView = view;
        return MIWindowHolder.INSTANCE;
    }


    private static class MIWindowHolder {
        private static final MiWindow INSTANCE = new MiWindow();
    }


    /**
     * Show the view for the specified duration.
     */
    public void show() {
        if (mView == null) {
            return;
        }
        if (isShow) {
            return;
        }
        if (toast == null) {
            toast = new Toast(mContext);
            toast.setView(mView);
        }
        initTN();
        try {
            show.invoke(mTN);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        isShow = true;
    }


    /**
     * Close the view if it's showing, or don't show it if it isn't showing yet.
     * You do not normally have to call this.  Normally view will disappear on its own
     * after the appropriate duration.
     */
    public void hide() {
        if (!isShow) return;
        try {
            hide.invoke(mTN);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        isShow = false;
        toast = null;
        mView = null;
        mTN = null;
        show = null;
        hide = null;
    }


    public void setView(View view) {
        this.mView = view;
    }


    public View getView() {
        return mView;
    }


    private void initTN() {
        try {
            Field tnField = toast.getClass().getDeclaredField("mTN");
            tnField.setAccessible(true);
            mTN = tnField.get(toast);
            show = mTN.getClass().getMethod("show");
            hide = mTN.getClass().getMethod("hide");

            Field tnParamsField = mTN.getClass().getDeclaredField("mParams");
            tnParamsField.setAccessible(true);
            WindowManager.LayoutParams params = (WindowManager.LayoutParams) tnParamsField.get(mTN);
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

            /**调用tn.show()之前一定要先设置mNextView*/
            Field tnNextViewField = mTN.getClass().getDeclaredField("mNextView");
            tnNextViewField.setAccessible(true);
            tnNextViewField.set(mTN, toast.getView());
        } catch (Exception e) {
            e.printStackTrace();
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

}